{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setting Up MongoDB"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First download and install MongoDB <b><u>Community Server</u></b> from below mentioned link<br>\n",
    "www.mongodb.org/downloads\n",
    "\n",
    "Installation Reference -->  https://www.youtube.com/watch?v=7-okpwPkEeg\n",
    "\n",
    "After that<br />\n",
    "<code>pip install pymongo</code>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SQL vs NoSQL\n",
    "\n",
    "SQL:\n",
    "* The model is of a relational nature\n",
    "* Data is stored in tables\n",
    "* Suitable for solutions where every record is of the same kind and possesses the same properties\n",
    "* Adding a new property means you have to alter the whole schema\n",
    "* The schema is very strict\n",
    "\n",
    "NoSQL:\n",
    "* The model is non-relational\n",
    "* May be stored as JSON, key-value, etc. (depending on type of NoSQL database)\n",
    "* Not every record has to be of the same nature, making it very flexible\n",
    "* Add new properties to data without disturbing anything\n",
    "* No schema requirements to adhere to\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Working with Mongodb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* MongoDB uses binary encoding of JSON called BSON.\n",
    "* Adds type and size information"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lets now open up the mongodb client (open cmd and run 'mongo' command) and try some operations:\n",
    "\n",
    "<code>show dbs</code> Show all the databases\n",
    "\n",
    "<code>use test_database</code> Switches to test_database. If not present, creates a new db\n",
    "\n",
    "<code>db</code> Shows current database\n",
    "\n",
    "<code>db.createCollection('employees')</code> Creates collection 'employees'\n",
    "\n",
    "<code>show collections</code> \n",
    "\n",
    "<code>db.employees.insert({})</code> Insert document to collection\n",
    "\n",
    "<code>db.employees.find()</code> Shows all the documents in a collection\n",
    "\n",
    "<code>db.employees.insert([{}, {}....])</code> Insert multiple documents to a collection\n",
    "\n",
    "<code>db.employees.find().pretty()</code> Shows all documents in a nice json format\n",
    "\n",
    "<code>db.employees.update({first_name:'kanav'},{first_name:'kanav', gender:'male'})</code> Update\n",
    "\n",
    "<code>db.employees.update({f_n:'kanav'}, {$set:{age:45}})</code> Better way to update\n",
    "\n",
    "<code>db.employees.update({f_n:'kanav'}, {$inc:{age:5}})</code> Increment\n",
    "\n",
    "<code>db.employees.remove({first_name:'kanav'})</code> Delete Document\n",
    "\n",
    "<code>db.employees.remove({first_name:'kanav'}, {justOne:true})</code> Delete Document\n",
    "\n",
    "<code>db.employees.count()</code> Count number of Documents\n",
    "\n",
    "<code>db.employees.find({age:{\\$lt:40}}).pretty()</code> \n",
    "Give us everybody less than 40 (gt, lte, gte)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Working with pymongo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-07T03:28:29.765621Z",
     "start_time": "2018-09-07T03:28:29.198955Z"
    }
   },
   "outputs": [],
   "source": [
    "import pymongo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-07T03:31:41.992350Z",
     "start_time": "2018-09-07T03:31:41.988361Z"
    }
   },
   "outputs": [],
   "source": [
    "# initiallize the MongoClient\n",
    "client = pymongo.MongoClient()\n",
    "\n",
    "# connecting to 'test_database'\n",
    "database = client['test_database']\n",
    "\n",
    "# creating the collection 'test_collection'\n",
    "collection = database['test_collection']\n",
    "\n",
    "# inserting one document\n",
    "collection.insert_one({'first_name':'kanav', 'last_name':'bansal'})\n",
    "\n",
    "\n",
    "# try insert([{},{},.....])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-07T03:35:11.024462Z",
     "start_time": "2018-09-07T03:35:11.018476Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'_id': ObjectId('5b91f0a0a4e77b48e449138b'),\n",
       " 'first_name': 'kanav',\n",
       " 'last_name': 'bansal'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# finding a document in the collection\n",
    "\n",
    "collection.find_one({'first_name':'kanav'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-06T18:55:22.009053Z",
     "start_time": "2018-09-06T18:55:21.850454Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\kanav\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages\\ipykernel_launcher.py:1: DeprecationWarning: update is deprecated. Use replace_one, update_one or update_many instead.\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'n': 1, 'nModified': 1, 'ok': 1.0, 'updatedExisting': True}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#updating the document in the collection\n",
    "\n",
    "collection.update({'first_name':'kanav'}, \\\n",
    "                  {'$set':{'first_name':'Kanav'}})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-06T19:10:11.461731Z",
     "start_time": "2018-09-06T19:10:11.392915Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\kanav\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages\\ipykernel_launcher.py:1: DeprecationWarning: count is deprecated. Use estimated_document_count or count_documents instead. Please note that $where must be replaced by $expr, $near must be replaced by $geoWithin with $center, and $nearSphere must be replaced by $geoWithin with $centerSphere\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# counting the number of document in the collection\n",
    "\n",
    "collection.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-07T03:43:04.565187Z",
     "start_time": "2018-09-07T03:43:04.558242Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pymongo.results.DeleteResult at 0x4b3dcb0>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# deleting the document from collection\n",
    "\n",
    "collection.delete_one({'first_name':'Kanav'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-09-07T03:44:29.849201Z",
     "start_time": "2018-09-07T03:44:29.842220Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'_id': ObjectId('5b91ed708536e04973c77507'), 'f_n': 'lmn', 'l_n': 'pqr'}\n",
      "{'_id': ObjectId('5b91f0a0a4e77b48e449138b'), 'first_name': 'kanav', 'last_name': 'bansal'}\n",
      "{'_id': ObjectId('5b91f0a0a4e77b48e449138c'), 'first_name': 'qwe', 'last_name': 'rty'}\n",
      "{'_id': ObjectId('5b91f0a0a4e77b48e449138d'), 'first_name': 'asd', 'last_name': 'fgh'}\n",
      "{'_id': ObjectId('5b91f0a0a4e77b48e449138e'), 'first_name': 'zxc', 'last_name': 'vbn'}\n"
     ]
    }
   ],
   "source": [
    "# Display all the document\n",
    "\n",
    "data = collection.find()\n",
    "\n",
    "for document in data:\n",
    "    print(document)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
